package com.mycjj.android.fragment;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.activity.My_StoreActivityDetatilActivity;
import com.mycjj.android.adapter.MainStoreActivityAdapter;
import com.mycjj.android.bean.StoreActivity;
import com.mycjj.android.bean.StoreActivityBaseBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.pullrefresh.PullToRefreshBase;
import com.mycjj.android.pullrefresh.PullToRefreshBase.Mode;
import com.mycjj.android.pullrefresh.PullToRefreshBase.OnRefreshListener2;
import com.mycjj.android.pullrefresh.PullToRefreshListView;
import com.mycjj.android.util.Preferences;

/**
 * 用品
 * 
 * @author mark
 * 
 */
public class ServiceActivityFragment extends BaseFragment {
	public static ServiceActivityFragment newInstance() {
		ServiceActivityFragment fragment = new ServiceActivityFragment();
		return fragment;
	}

	private View rootView;
	private PullToRefreshListView mListView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_main_new_car, null);
		mListView = (PullToRefreshListView) rootView
				.findViewById(R.id.lv_pull_refresh_list);
		mListView.setMode(Mode.BOTH);
		mListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				Toast.makeText(getActivity(), "下拉刷新", Toast.LENGTH_LONG).show();
				initData(0, false);
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				if (isHasNextData) {// 有第二页数据
					index++;
					initData(index, false);
				} else {
					Toast.makeText(getActivity(), "没有更多的数据需要显示!",
							Toast.LENGTH_LONG).show();
					mListView.onRefreshComplete();
				}
			}

		});

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getActivity(),
						My_StoreActivityDetatilActivity.class);
				if (null != dataList) {
					StoreActivity bean = dataList.get(position);
					intent.putExtra("activity", bean);
					intent.putExtra("id", bean.getId());
					startActivity(intent);
				}
			}
		});
		return rootView;
	}

	private boolean isHasNextData;
	private int index = 1;
	private MainStoreActivityAdapter adapter;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initData(0, false);
	}

	List<StoreActivity> dataList;

	private void initData(int index, boolean isShow) {
		adapter = new MainStoreActivityAdapter(getActivity());

		ProjectHttpRequestInterface request = new ProjectHttpRequestImpl(
				getActivity(), true);
		RequestParams params = new RequestParams();
		params.put("page", String.valueOf(index));
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		String storeid = Preferences.getSharedPreferences(getActivity(),
				"storeId", "");
		params.put("store_id", storeid);
		params.put("token", Preferences.getSharedPreferences(getActivity(),
				"user_token", ""));
		request.requestStoreGetActivityWithDoneHandler("", params,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						StoreActivityBaseBean activityBean = ProjectDataManage
								.getInstance().getStoreActivityBaseBean();
						Log.d("PointActivityFragment",
								(null == activityBean) ? "activityBean is null"
										: "activityBean code"
												+ activityBean.getCode());
						if ((null != activityBean)
								&& (null != activityBean.getData())) {
							isHasNextData = activityBean.getData()
									.isHas_next_page();
							dataList = activityBean.getData().getList();
							adapter.setList(dataList);
						}
						mListView.onRefreshComplete();
					}

				});

		mListView.setAdapter(adapter);
	}
}
